package bocchi.general;

import bocchi.BocchiCore;
import bocchi.patch.PlayerEnum;
import com.google.gson.JsonObject;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.localization.CardStrings;

public class CardData {
    public final String ID;
    public final String NAME;
    public final String DESCRIPTION;
    public final String UPGRADE_DESCRIPTION;
    public final String IMG_PATH;
    public final int COST;
    public final int UPGRADE_COST;
    public final AbstractCard.CardType TYPE;
    public final AbstractCard.CardColor COLOR;
    public final AbstractCard.CardRarity RARITY;
    public final AbstractCard.CardTarget TARGET;
    public final int DAMAGE;
    public final int UPGRADE_DAMAGE;
    public final int BLOCK;
    public final int UPGRADE_BLOCK;
    public final int MAGIC_NUMBER;
    public final int UPGRADE_MAGIC_NUMBER;

    public CardData(String rawID, JsonObject json) {
        ID = rawID;

        CardStrings cardStrings = CardCrawlGame.languagePack.getCardStrings(BocchiCore.getGameID(rawID));
        NAME = cardStrings.NAME;
        DESCRIPTION = cardStrings.DESCRIPTION;
        UPGRADE_DESCRIPTION = cardStrings.UPGRADE_DESCRIPTION;

        COST = getInt(json, "COST");
        UPGRADE_COST = getInt(json, "UPGRADE_COST", COST);

        TYPE = getEnum(json, "TYPE", AbstractCard.CardType.class);
        RARITY = getEnum(json, "RARITY", AbstractCard.CardRarity.class);
        TARGET = getEnum(json, "TARGET", AbstractCard.CardTarget.class);
        COLOR = getEnum(json, "COLOR", AbstractCard.CardColor.class, PlayerEnum.HITORI_PINK);

        DAMAGE = getInt(json, "DAMAGE", 0);
        BLOCK = getInt(json, "BLOCK", 0);
        MAGIC_NUMBER = getInt(json, "MAGIC_NUMBER", 0);
        UPGRADE_DAMAGE = getInt(json, "UPGRADE_DAMAGE", DAMAGE);
        UPGRADE_BLOCK = getInt(json, "UPGRADE_BLOCK", BLOCK);
        UPGRADE_MAGIC_NUMBER = getInt(json, "UPGRADE_MAGIC_NUMBER", MAGIC_NUMBER);

        String imagePath = ResourcePath.card(rawID);
        IMG_PATH = ResourceLoader.has(imagePath) ? imagePath : ResourcePath.defaultCard(TYPE);
    }

    private static int getInt(JsonObject json, String key) {
        return getInt(json, key, 0);
    }

    private static int getInt(JsonObject json, String key, int defaultValue) {
        return json.has(key) ? json.get(key).getAsInt() : defaultValue;
    }

    private static <T extends Enum<T>> T getEnum(JsonObject json, String key, Class<T> enumClass) {
        return getEnum(json, key, enumClass, null);
    }

    private static <T extends Enum<T>> T getEnum(JsonObject json, String key, Class<T> enumClass, T defaultValue) {
        return json.has(key) ? Enum.valueOf(enumClass, json.get(key).getAsString()) : defaultValue;
    }
}
